<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 商品表控制器
 * 通过 id 获取 单条数据
 * @author 周英勇 15913362842@163.com
 */
class Goods extends Web_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('activity_model','activity_model');
        $this->load->model('Goods_model','goods_model');
    }
	
	/**
	*商品banner图列表
	*输出上架商品的banner活动图
	*根据创建时间排序
	*/
	public function index()
	{   
        $data['user'] = $this->userSession(false);
        $data['base_path'] = $this->config->item('base_url').'static/upload/goods/';
		//$activity_id = $this->input->get('activity_id');
        //$goods_name = $this->input->get('goods_name');
        $data['info'] = $this->goods_model->getOnlineGoodsList();
        if(!empty($data['info'])){
            foreach($data['info'] as $key => $value){
                $res = $this->JsonsToArray($value['banner_img']);
                $data['info'][$key]['img_path'] = isset($res['img_path'][0])?$res['img_path'][0]:'';
                $data['info'][$key]['img_name'] = isset($res['img_path'][0])?$res['img_path'][0]:'';
            }
        }

        //$data['activity'] = $this->activity_model->getActivitysByStatus();
        $data['base_url'] = $this->base_url;
        $this->loadView('header', $data);
		$this->loadView('index');
	}
 

    /*
    *把一条地址图片名称关联的json数组转换成数组
    *params $json
    *data $array['img_path']:图片路径,$array['img_name']:图片名;
    */
    public function JsonSToArray($json){
        $res = json_decode($json,true);
        $data = array();
        if(!empty($res)){
            foreach($res as $key => $value){                
                $data['img_path'][] = $key;
                $data['img_name'][] = $value;
            }
        }

        return $data;
    }
   /**

   /**
    *商品详情
    */
    public function goodsDetail(){
        $data['user'] = $this->userSession(false);
        $goods_id = $this->input->get('goods_id');
        $data['base_path'] = $this->config->item('base_url').'static/upload/goods/';
        if(isset($goods_id)){
            $data['info'] = $this->goods_model->getGoodsById($goods_id);
            $data['info']['ads_img'] = $this->JsonsToArray($data['info']['ads_img']);
        }else{
            $data['info'] = array('name'=>'','other_name'=>'','price'=>'','descript'=>'','banner_img'=>'','ads_img'=>'','status'=>'on','goods_detail'=>'','goods_params'=>'','goods_id'=>'','activity_id'=>'');
        }
        $data['base_url'] = $this->base_url;
        $data['activity'] = $this->activity_model->getActivitysById($data['info']['activity_id']);
        $data['goods_id'] = $goods_id;
        $data['from'] = $this->input->get('from');//可能从多个页面进来,from用来判断后退返回到哪里
        $this->loadView('header', $data);
        if($data['info']['status'] == '1'){
            header('Location:'.$this->base_url.'home/goods/index'); return;
        }
        $this->loadView('goods_detail');
           
    }
}
